<template>
  <div class="app-container bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view />
    </transition>
    <el-row v-if="isShow" :gutter="20" class="model">
      <el-col :xs="8" :sm="7" :md="6" :lg="6" :xl="4">
        <el-card>
          <span>合计</span>
          <div><span>￥ </span> {{ info.total_money }}</div>
        </el-card>
      </el-col>
      <el-col :xs="8" :sm="7" :md="6" :lg="6" :xl="4">
        <el-card>
          <span>今日合计</span>
          <div><span>￥ </span> {{ info.today_total }}</div>
        </el-card>
      </el-col>
      <el-col :xs="8" :sm="7" :md="6" :lg="6" :xl="4">
        <el-card>
          <span>昨日合计</span>
          <div><span>￥ </span> {{ info.yesterday_total }}</div>
        </el-card>
      </el-col>
    </el-row>

    <el-row v-if="isShow" :gutter="20">
      <el-col :span="24">
        <el-card class="box-1">
          <div slot="header" class="header">
            <el-select v-model="ranklistType" class="select" @change="changeRanklistType">
              <el-option :value="0" label="销售人员排行榜" />
              <el-option :value="1" label="部门排行榜" />
              <el-option :value="2" label="团队排行榜" />
            </el-select>
            <el-select v-if="!ranklistType" v-model="searchTerms.company_id" placeholder="部门" class="select" clearable @change="getData">
              <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-select v-if="!ranklistType" v-model="searchTerms.branch_id" placeholder="团队" class="select" clearable @change="getData">
              <el-option v-for="item in branchList" :key="item.id" :label="item.branch_name" :value="item.id" />
            </el-select>
            <el-select v-if="!ranklistType" v-model="searchTerms.admin_id" placeholder="销售人员" class="select" filterable clearable @change="getData">
              <el-option v-for="item in usersList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-date-picker v-model="searchTerms.date" class="picker" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeRanklistType" />
          </div>
          <ul class="rankbar">
            <li v-for="(item, index) in ranklist123" :key="index" @click="!ranklistType?memberClick(item.id, item.name):''">
              <el-card shadow="hover">
                <div class="content">
                  <div class="text">
                    <h1>{{ item.name }} <span v-if="!ranklistType"> ({{ item.company_name }})</span></h1>
                    <div><span>￥ </span> {{ item.total_money }}</div>
                  </div>
                  <div class="image">
                    <img :src="item.wechat_pic?(item.wechat_pic.indexOf('http')>=0?item.wechat_pic:imageUrl+item.wechat_pic):defaultImage" alt="">
                    <img :src="index==0?no1_img:index==1?no2_img:no3_img" class="num-img" alt="">
                  </div>
                </div>
              </el-card>
            </li>
          </ul>
          <ul class="ranklist">
            <li v-for="(item, index) in ranklist" :key="index" @click="!ranklistType?memberClick(item.id, item.name):''">
              <img :src="item.wechat_pic?(item.wechat_pic.indexOf('http')>=0?item.wechat_pic:imageUrl+item.wechat_pic):defaultImage" alt="">
              <h1>{{ item.name }} <span v-if="!ranklistType"> ({{ item.company_name }})</span></h1>
              <el-progress :percentage="item.percentage" :show-text="false" :stroke-width="16" color="#1EC2A0" />
              <div>
                <span>￥ </span> {{ item.total_money }}
                <!--<img :src="item.status==1?up_img:down_img" alt="">-->
              </div>
            </li>
          </ul>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import defaultImage from '../../images/default_avatar.png'
import no1Img from '../../images/no1.png'
import no2Img from '../../images/no2.png'
import no3Img from '../../images/no3.png'
import upImg from '../../images/up.png'
import downImg from '../../images/down.png'

export default {
  data() {
    return {
      // imageUrl: this.$store.getters.serverUrl,
imageUrl: '',
      defaultImage: defaultImage,
      no1_img: no1Img,
      no2_img: no2Img,
      no3_img: no3Img,
      up_img: upImg,
      down_img: downImg,

      isShow: false,

      ranklistType: 0,
      searchTerms: {
        branch_id: '',
        company_id: '',
        admin_id: '',
        date: []
      },

      info: {},
      branchList: [],
      companyList: [],
      usersList: [],
      ranklist123: [],
      ranklist: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '业绩排行榜') {
        this.getData();
        setTimeout(() => {
          this.isShow = true;
        }, 500)
      } else {
        this.isShow = false;
      }
    }
  },
  created() {
    if (this.$route.name === '业绩排行榜') {
      this.isShow = true;
      this.getData();
      this.getBranchList();
      this.getCompanyList();
      this.getUsersList();
    }
  },
  methods: {
    changeRanklistType() {
      if (this.ranklistType === 0) {
        this.getData();
      } else {
        this.getTeamData();
      }
    },
    getData() {
      const url = 'statistic/rank-list'
      const data = {
        typeid: 1,
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue,
        branch_id: this.searchTerms.branch_id,
        company_id: this.searchTerms.company_id,
        admin_id: this.searchTerms.admin_id
      }
      this.searchTerms.date && this.searchTerms.date.length > 0 && (data.start_time = this.searchTerms.date[0] + ' 00:00:00', data.end_time = this.searchTerms.date[1] + ' 23:59:59');
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        res.data.list.forEach(item => {
          res.data.list[0].total_money === 0 ? (item.percentage = 0) : (item.percentage = item.total_money / res.data.list[0].total_money * 100);
          item.percentage === 0 && (item.percentage = 0.1);
        })
        this.ranklist = res.data.list;
        this.ranklist123 = res.data.list.slice(0, 3);
        this.info = {
          today_total: res.data.today_total,
          total_money: res.data.total_money,
          yesterday_total: res.data.yesterday_total
        }
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    getTeamData() {
      const url = 'statistic/team-list';
      const data = {};
      this.searchTerms.date && (data.start_time = this.searchTerms.date[0], data.end_time = this.searchTerms.date[1]);
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        if (this.ranklistType === 1) {
          res.data.company.forEach(item => {
            res.data.company[0].total_money === 0 ? (item.percentage = 0) : (item.percentage = item.total_money / res.data.company[0].total_money * 100)
            item.percentage === 0 && (item.percentage = 0.1)
          })
          this.ranklist = res.data.company;
          this.ranklist123 = res.data.company.slice(0, 3);
        } else {
          res.data.branchs.forEach(item => {
            res.data.branchs[0].total_money === 0 ? (item.percentage = 0) : (item.percentage = item.total_money / res.data.branchs[0].total_money * 100);
            item.percentage === 0 && (item.percentage = 0.1);
          })
          this.ranklist = res.data.branchs;
          this.ranklist123 = res.data.branchs.slice(0, 3);
        }
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    /**
     *  获取部门列表
     *  接口:'company/all-list'
     */
    getCompanyList() {
      const url = 'company/all-list';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.companyList = res.data;
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    /**
     *  获取团队列表
     *  接口:'branch/all-list'
     */
    getBranchList() {
      const url = 'branch/all-list';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.branchList = res.data;
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    /**
     *  获取系统用户列表
     *  接口:'user/all-list'
     */
    getUsersList() {
      const url = 'user/all-list';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.usersList = res.data;
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    memberClick(id, name) {
      this.isShow = false;
      const query = { id: id, name: name };
      this.searchTerms.date && this.searchTerms.date.length > 0 && (query.startTime = this.searchTerms.date[0]);
      this.searchTerms.date && this.searchTerms.date.length > 1 && (query.endTime = this.searchTerms.date[1]);
      this.$router.push({
        path: '/workbench/sign_in/personalty',
        query: query
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../styles/index.scss';
.model {
  .el-card {
    color: #fff;
    background-color: $panGreen;
    span {
      font-size: 14px;
    }
    div {
      font-size: 24px;
      margin-top: 10px;
    }
  }
  .el-card:hover {
  }
}
.box-1 {
  @extend .bg-white;
  @include margin(30px);
  .header {
    @include flex-row(center, flex-start);
    .select {
      padding-right: 15px;
      width: 180px;
    }
    .picker {
      margin-right: 15px;
      width: 410px;
    }
    .btn {
      width: 100px;
      background-color: $panGreen;
      border: none;
    }
  }
  .rankbar {
    list-style: none;
    @include flex-row(center, flex-start);
    padding: 0;
    > li {
      margin-right: 20px;
      .el-card {
        width: 260px;
        cursor: pointer;
        .content {
          @include flex-row(center, space-between);
          .text {
            h1 {
              font-size: 16px;
              font-weight: normal;
              > span {
                color: $textGray;
                font-weight: normal;
              }
            }
            div {
              font-size: 23px;
              span {
                font-size: 14px;
                font-weight: normal;
              }
            }
          }
          .image {
            position: relative;
            img {
              width: 70px;
              height: 70px;
              border-radius: 70px;
              margin-left: 15px;
            }
            .num-img {
              position: absolute;
              top: 0;
              right: 0;
              width: 30px;
              height: 30px;
            }
          }
        }
      }
    }
  }
  .ranklist {
    list-style: none;
    padding: 20px 0 0 0;
    > li {
      @include flex-row(center, flex-start);
      padding: 10px 0;
      cursor: pointer;
      > img {
        width: 40px;
        height: 40px;
        border-radius: 20px;
        margin-right: 15px;
      }
      > h1 {
        width: 150px;
        font-size: 16px;
        font-weight: normal;
        > span {
          color: $textGray;
          font-weight: normal;
        }
      }
      .el-progress {
        width: 50%;
        padding: 0 20px;
      }
      > div {
        font-size: 18px;
        color: $textGray;
        line-height: 40px;
        > span {
          font-size: 14px;
        }
        > img {
          width: 15px;
          height: 15px;
        }
      }
    }
  }
}
</style>
<style>
.el-progress-bar__outer {
  background-color: #fff;
}
</style>

